package com.mijie.homi.service.timeline;

import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mijie.exception.DeleteLastOneRecordException;
import com.mijie.exception.RecordNotFoundException;
import com.mijie.exception.UpdateFailException;
import com.mijie.homi.common.domain.mood.Mood;
import com.mijie.homi.common.domain.timeline.TimelineEvent;
import com.mijie.homi.common.domain.user.UserExtInfo;
import com.mijie.homi.dao.timeline.TimelineDao;
import com.mijie.homi.service.mood.MoodService;
import com.mijie.homi.service.topic.TopicService;
import com.mijie.homi.service.user.UserExtInfoService;

@Service
public class TimelineService {

	@Autowired
	private TimelineDao dao;
	@Autowired
	private UserExtInfoService userExtInfoService;
	@Autowired
	private MoodService moodService;
	@Autowired
	private TopicService topicService;
	
	@Transactional(rollbackFor={Exception.class})
	public int addTimelineEvent(TimelineEvent event){
		event.setCreateAt(new Date().getTime());
		return dao.addTimelineEvent(event);
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int deleteTopicEvent(int userId,int timelineId){
		return dao.deleteTimelineEvent(userId, timelineId);
	}
	
	@Transactional(rollbackFor={Exception.class})
	public int deleteTimelineEvent(StringBuilder msg,int userId,int id) 
			throws RecordNotFoundException, DeleteLastOneRecordException, UpdateFailException{
		TimelineEvent event = dao.getTimelineEvent(userId, id);
		if(event==null) throw new RecordNotFoundException(id);
		
		int succ =  dao.deleteTimelineEvent(event.getUserId(), event.getId());
		if(succ>0){
			if(event.getEventType()==TimelineEvent.TYPE_MOOD){
				UserExtInfo extInfo = userExtInfoService.getUserExtInfoByUserId(event.getUserId());
				if(extInfo.getNumOfMood()<=1) throw new DeleteLastOneRecordException(userId,id);
				
				if(extInfo.getMoodId()==event.getEventId()){
					Mood prevMood = moodService.getPrevMoodById(event.getUserId(),event.getEventId());
					if(prevMood==null){
						throw new DeleteLastOneRecordException(userId,id);
					}else{
						succ = moodService.deleteMood(event.getEventId(), event.getUserId());
						if(succ>0){
							succ = userExtInfoService.settingMoodAndDecrNumOfMood(event.getUserId(), prevMood.getMood(), prevMood.getId());
							if(succ==0) throw new UpdateFailException("settingMoodAndDecrNumOfMood fail.user:"+event.getUserId()+",moodId:"+prevMood.getId());
							JSONObject json = JSONObject.fromObject(prevMood);
							json.put("createAt", prevMood.getCreateAt().getTime());
							msg.append(json.toString());
						}
					}
				}else{
					Mood mood = moodService.getMoodById(event.getEventId());
					if(mood==null) throw new RecordNotFoundException(event.getEventId());
					JSONObject json = JSONObject.fromObject(mood);
					json.put("createAt", mood.getCreateAt().getTime());
					msg.append(json.toString());
					succ = moodService.deleteMood(event.getEventId(), event.getUserId());
					if(succ>0)
						userExtInfoService.decrNumOfMood(event.getUserId());
				}
			}else if(event.getEventType()==TimelineEvent.TYPE_TOPIC){
				topicService.anonymousTopic(event.getUserId(), (int)event.getEventId());
			}
		}
		return succ;
	}
	
	
	public List<TimelineEvent> timelineSegment(int userId,Integer sinceId,Integer maxId,int count){
		return dao.timelineSegment(userId, sinceId, maxId, count);
	}
	
}
